//
//  WSOrderEvaluateView.m
//  FD
//
//  Created by xiaofang wang on 2018/10/31.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  评价view(头像，姓名，评分)  一个电话按钮
// h  80
#import "WSOrderEvaluateView.h"
#import "XHStarRateView.h"

#import "FDSystemTool.h" //拨打电话

@interface WSOrderEvaluateView()
/**1.头像*/
@property (nonatomic, strong) UIImageView *iconIv;


//这三个view 放在一起
/**div1*/
@property (nonatomic, strong) UIView *div1;
/**2.姓名*/
@property (nonatomic, strong) UILabel *nameLb;
/**3.评分*/
@property (nonatomic, strong) UILabel *pinFenLb;
/**4.评星view*/
@property (nonatomic, strong) XHStarRateView *starView;


/**5.电话*/
@property (nonatomic, strong) UIButton *makePhoneBtn;
/**2019-2-14记录骑手电话用于打电话*/
@property (nonatomic, strong) NSString *tempRiderPhone;


@end
@implementation WSOrderEvaluateView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView
{
    //白色背景
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.iconIv];
    
    [self addSubview:self.div1];
    [self.div1 addSubview:self.nameLb];
    [self.div1 addSubview:self.pinFenLb];
    [self.div1 addSubview:self.starView];
    
    [self addSubview:self.makePhoneBtn];
}
//!!!!:layout
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //头像
    [self.iconIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 60));
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.mas_left).offset(15);
    }];
    
    
    
    [self.div1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconIv.mas_right).offset(5);
        make.centerY.equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(180, 50));
    }];
    [self singleLayoutNamePinFenStarView];

    
    [self.makePhoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-10);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];

}

//50
/**单独布局 姓名、评分、星星  21+21*/
- (void)singleLayoutNamePinFenStarView
{
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div1.mas_left);
        make.top.equalTo(self.div1.mas_top).offset(4);
        make.size.mas_equalTo(CGSizeMake(60, 21));
    }];
    [self.pinFenLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div1.mas_left);
        make.top.equalTo(self.nameLb.mas_bottom).offset(2);
        make.size.mas_equalTo(CGSizeMake(50, 21));
    }];
    [self.starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pinFenLb.mas_top);
        make.left.equalTo(self.pinFenLb.mas_right).offset(2);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
}


//!!!!:getter
- (UIImageView *)iconIv { //切圆角
    if (!_iconIv) {
        _iconIv = [[UIImageView alloc] init];
//        _iconIv.image = [UIImage imageNamed:@"placeholderImage"];
        //2019-2-18不要假数据
    
        _iconIv.layer.cornerRadius = 30;
        _iconIv.layer.masksToBounds = YES;
    }
    return _iconIv;
}
- (UIView *)div1 {
    if (!_div1) {
        _div1 = [UIView new];
        _div1.backgroundColor = [UIColor whiteColor];
    }
    return _div1;
}

- (UILabel *)nameLb {
    if (!_nameLb) {
        _nameLb = [UILabel new];
//        _nameLb.text = @"王小明"; //2019-2-18不要假数据
    }
    return _nameLb;
}
- (UILabel *)pinFenLb {
    if (!_pinFenLb) {
        _pinFenLb = [UILabel new];
        //国际化
        NSString *pfStr = NSLocalizedString(Grade, @"评分");
        _pinFenLb.text = pfStr;
    }
    return _pinFenLb;
}
- (XHStarRateView *)starView {
    if (!_starView) {
        _starView = [[XHStarRateView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
        //2019-2-18 评分只能看不能点
        _starView.userInteractionEnabled = NO;
    }
    return _starView;
}

- (UIButton *)makePhoneBtn {
    if (!_makePhoneBtn) {
        _makePhoneBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_makePhoneBtn setImage:[UIImage imageNamed:@"dianhua"] forState:(UIControlStateNormal)];
        [_makePhoneBtn addTarget:self action:@selector(clickMakePhoneBntAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _makePhoneBtn;
}


//!!!!:点击事件
- (void)clickMakePhoneBntAction:(UIButton *)btn {
    FDLog(@"点击了联系顾客");
    
    if ([NSString isEmptyString:self.tempRiderPhone]) {
        FDLog(@"骑手电话为空无法拨打");
    }else {
        [FDSystemTool fd_makePhone:self.tempRiderPhone];
    }
}


#pragma mark - 12-25新加接口请求骑手信息
/**
 {"event":"SUCCESS","data":{"head":"public/a5ec706af14d4514ba7af67dbc1d29c5","complexStarLevel":5,"phone":"13656690321","name":"spring","id":1},"describe":""}
 */
- (void)requestRiderMessageWithOrderId:(NSString *)orderId {
    [FDHttpAPi requestOrderDetailRiderMessageWithOrderId:orderId successBlock:^(id responseObjc) {
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            NSDictionary *riderMsgDic = responseObjc[@"data"];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self updateEvaluteViewWithDic:riderMsgDic];
            });

        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
        
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}

//更新骑手信息
- (void)updateEvaluteViewWithDic:(NSDictionary *)dic {
    NSString *head = dic[@"head"];//头像
    NSNumber *complexStarLevel = dic[@"complexStarLevel"]; //5星好评
    NSString *phone = dic[@"phone"]; //电话
    
    self.tempRiderPhone = phone;
    
    NSString *name = dic[@"name"]; //姓名
    NSURL *headurl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicDomain,head]];

    [self.iconIv sd_setImageWithURL:headurl placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    [self.iconIv sd_setImageWithURL:headurl placeholderImage:[UIImage imageNamed:@"placeholderImage"]];

    
    self.nameLb.text = name;
    self.starView.currentRating = [complexStarLevel floatValue]; //5星好评
}

@end
